package com.skyline.wizard.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.lucene.document.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.skyline.wizard.dao.DocDao;
import com.skyline.wizard.mapper.ArticleDocMapper;
import com.skyline.wizard.mapper.UserDocMapper;
import com.skyline.wizard.type.SeDocType;

@Repository("docDao")
public class DocDaoImpl implements DocDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Value("${UserDao.queryAllNewUser}")
	private String queryAllNewUserSql;

	@Value("${UserDao.queryAllUpdatedUser}")
	private String queryAllUpdatedUserSql;

	@Value("${UserDao.queryAllDeletedUser}")
	private String queryAllDeletedUserSql;
	
	@Value("${UserDao.queryAllUser}")
	private String queryAllUserSql;

	@Value("${ArticleDao.queryAllNewArticle}")
	private String queryAllNewArticleSql;

	@Value("${ArticleDao.queryAllUpdatedArticle}")
	private String queryAllUpdatedArticleSql;

	@Value("${ArticleDao.queryAllDeletedArticle}")
	private String queryAllDeletedArticleSql;
	
	@Value("${ArticleDao.queryAllArticle}")
	private String queryAllArticleSql;

	@Value("${PhotoDao.queryAllNewPhoto}")
	private String queryAllNewPhotoSql;

	@Value("${PhotoDao.queryAllUpdatedPhoto}")
	private String queryAllUpdatedPhotoSql;

	@Value("${PhotoDao.queryAllDeletedPhoto}")
	private String queryAllDeletedPhotoSql;
	
	@Value("${PhotoDao.queryAllPhoto}")
	private String queryAllPhotoSql;

	@Value("${SpotDao.queryAllNewSpot}")
	private String queryAllNewSpotSql;

	@Value("${SpotDao.queryAllUpdatedSpot}")
	private String queryAllUpdatedSpotSql;

	@Value("${SpotDao.queryAllDeletedSpot}")
	private String queryAllDeletedSpotSql;
	
	@Value("${SpotDao.queryAllSpot}")
	private String queryAllSpotSql;

	@Override
	public List<Document> queryAllDocument(int seDocType){
		String sql = null;
		RowMapper<Document> mapper = null;
		switch (seDocType) {
		case SeDocType.ARTICLE:
			sql = queryAllArticleSql;
			mapper = ArticleDocMapper.getMapper();
			break;
		case SeDocType.PHOTO:
			sql = queryAllPhotoSql;
			break;
		case SeDocType.SPOT:
			sql = queryAllSpotSql;
			break;
		case SeDocType.USER:
			sql = queryAllUserSql;
			mapper = UserDocMapper.getMapper();
			break;
		}
		if (sql == null || mapper == null) {
			return null;
		} else {
			return jdbcTemplate.query(sql, mapper);
		}
	}
	
	@Override
	public List<Document> queryAllNewDocument(Date date, int seDocType) {
		String sql = null;
		RowMapper<Document> mapper = null;
		switch (seDocType) {
		case SeDocType.ARTICLE:
			sql = queryAllNewArticleSql;
			mapper = ArticleDocMapper.getMapper();
			break;
		case SeDocType.PHOTO:
			sql = queryAllNewPhotoSql;
			break;
		case SeDocType.SPOT:
			sql = queryAllNewSpotSql;
			break;
		case SeDocType.USER:
			sql = queryAllNewUserSql;
			mapper = UserDocMapper.getMapper();
			break;
		}
		if (sql == null || mapper == null) {
			return null;
		} else {
			return jdbcTemplate.query(sql, mapper, date);
		}
	}

	@Override
	public List<Document> queryAllUpdatedDocument(Date date, int seDocType) {
		String sql = null;
		RowMapper<Document> mapper = null;
		switch (seDocType) {
		case SeDocType.ARTICLE:
			sql = queryAllUpdatedArticleSql;
			mapper = ArticleDocMapper.getMapper();
			break;
		case SeDocType.PHOTO:
			sql = queryAllUpdatedPhotoSql;
			break;
		case SeDocType.SPOT:
			sql = queryAllUpdatedSpotSql;
			break;
		case SeDocType.USER:
			sql = queryAllUpdatedUserSql;
			mapper = UserDocMapper.getMapper();
			break;
		}
		if (sql == null || mapper == null) {
			return null;
		} else {
			return jdbcTemplate.query(sql, mapper, date);
		}
	}

	@Override
	public List<Document> queryAllDeletedDocument(Date date, int seDocType) {
		String sql = null;
		RowMapper<Document> mapper = null;
		switch (seDocType) {
		case SeDocType.ARTICLE:
			sql = queryAllDeletedArticleSql;
			mapper = ArticleDocMapper.getMapper();
			break;
		case SeDocType.PHOTO:
			sql = queryAllDeletedPhotoSql;
			break;
		case SeDocType.SPOT:
			sql = queryAllDeletedSpotSql;
			break;
		case SeDocType.USER:
			sql = queryAllDeletedUserSql;
			mapper = UserDocMapper.getMapper();
			break;
		}
		if (sql == null || mapper == null) {
			return null;
		} else {
			return jdbcTemplate.query(sql, mapper, date);
		}
	}

}
